package com.pcbly.web.action.login;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.pcbly.web.Constants;
import com.pcbly.web.action.BaseAction; 
import com.pcbly.web.bean.user.UserBean;
import com.pcbly.web.business.user.UserService;
import com.pcbly.web.components.utils.security.MD5;

public class LoginAction extends BaseAction<UserBean> {

	private static final long serialVersionUID = -9060123962044670166L;

	private UserService userService;

	private UserBean userBean;

	private String username;
	private String password;
	private String j_captcha_response;

	// private List<RoleBean> roleBeans;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getJ_captcha_response() {
		return j_captcha_response;
	}

	public void setJ_captcha_response(String j_captcha_response) {
		this.j_captcha_response = j_captcha_response;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public boolean validateLogin() {
		String rand = String.valueOf(getSession().getAttribute("rand"));
		
		if (username == null || "".equals(username.trim())) {
			addFieldError("username", getText("login.error.nonuser"));
			return false;
		}
		if (password == null || "".equals(password.trim())) {
			addFieldError("password", getText("login.error.nonpassword"));
			return false;
		}
		if (j_captcha_response == null || "".equals(j_captcha_response.trim())
				|| !rand.equals(j_captcha_response)) {
			addFieldError("validateId", getText("login.error.invalidValidateId"));
			return false;
		}
		return true;
	}

	public String login() throws Exception {
		if (log.isDebugEnabled())
			log.debug("login login begin..............");
		if(!validateLogin()){
			return ERROR;
		}
		HttpServletRequest request = getRequest();
		HttpSession session = getSession();

		// String loginName = request.getParameter("username");
		// String password = request.getParameter("password");
		// String captcha = request.getParameter("j_captcha_response");

		clearActionErrors();
		clearFieldErrors();
		setPassword(MD5.getMD5(password.trim().getBytes()));
		
		if (log.isDebugEnabled())
			log.debug(password);
		try {
			userBean = userService.findUserBeanByUserAndPasswd(username.trim(),
					password);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
			log.error(e.getMessage());
		}
		if (userBean == null) {
			addActionError(getText("login.error.invalidlogin"));
			return ERROR;
		}
		if (userBean.isAccountNonLocked()) {
			addActionError(getText("login.error.islock"));
			return ERROR;
		}

		session.removeAttribute(Constants.USER_SESSION_KEY);
		// if (getSession().getAttribute(Constants.USER_IN_SESSION)==null) {

		session.setAttribute(Constants.USER_SESSION_KEY, userBean);
		userBean.setLastLoginTime(new Date());
		userBean.setLoginTime(new Date());
		userBean.setLoginTimes(userBean.getLoginTimes() + 1);
		userBean.setLoginIP(request.getRemoteAddr());
		userBean.setLastLoginIP(request.getRemoteAddr());
		baseService.saveObject(userBean);
		// }
		if (log.isDebugEnabled())
			log.debug("login login end..............");
		return SUCCESS;
	}

}
